package com.zjw.zy.coreui.fragment.holder

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.graphics.Bitmap
import android.opengl.Visibility
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.zjw.zy.R
import com.zjw.zy.bean.MemorandumItem
import com.zjw.zy.base.BaseViewHolder
import com.zjw.zy.base.GestureRecyclerView
import com.zjw.zy.base.GestureViewHolder
import com.zjw.zy.coreui.fragment.adapter.MemorandumAdapter
import com.zjw.zy.coreui.view.SearchView
import com.zjw.zy.ext.alertConfirm
import com.zjw.zy.ext.dateString
import com.zjw.zy.utils.*
import kotlin.math.min

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/9/30 9:45
 * @description：
 */
class MemorandumHolder(itemView: View) : GestureViewHolder<MemorandumItem>(itemView) {

    companion object {
        const val DELETE_EVENT = "DELETE_EVENT"
        const val EDIT_EVENT = "EDIT_EVENT"
    }

    private var mBitmap: Bitmap? = null

    private val mTitle: TextView by lazy { findViewById(R.id.memorandum_item_title) }
    private val mDate: TextView by lazy { findViewById(R.id.memorandum_item_date) }
    private val mPreview: TextView by lazy { findViewById(R.id.memorandum_item_preview) }
    private val mDeleteBtn: ImageView by lazy { findViewById(R.id.memorandum_item_delete) }
    private val mRightBtnGroup: View by lazy { findViewById(R.id.memorandum_item_right_btn_group) }
    private val mLeft: View by lazy { findViewById(R.id.memorandum_item_left_contain) }
    private val mImage: ImageView by lazy { findViewById(R.id.memorandum_item_iamge) }
    override fun onBindListener() {
    }

    override fun onBindData(data: MemorandumItem?) {
        data?.let {
            mTitle.text = it.title
            mDate.text = it.date.dateString()
            mPreview.text = it.text.replace(Regex(PathUtils.IMAGE_PATTERN.pattern()), "[图片]")

            PathUtils.findImageUrlFromString(it.text).apply {
                if (this.size != 0) {
                    mBitmap?.recycle()
                    mBitmap = PathUtils.getBitmapFromPath(this[0])
                    mImage.setImageBitmap(mBitmap)
                    mImage.visibility = View.VISIBLE
                } else {
                    mImage.visibility = View.GONE
                }
            }
        }
    }

    override fun onUpSlide(parent: GestureRecyclerView, event: MotionEvent) {

    }

    override fun onDownSlide(parent: GestureRecyclerView, event: MotionEvent) {

    }

    override fun onLeftSlide(parent: GestureRecyclerView, event: MotionEvent) {
        //左滑出现删除按钮
        expand()
    }

    override fun onRightSlide(parent: GestureRecyclerView, event: MotionEvent) {
        //右滑复原
        unExpand()
    }

    private fun expand() {
        AnimationUtils.objectAnimationOfX(mLeft, -150f)
            .addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator?, isReverse: Boolean) {
                    mRightBtnGroup.visibility = View.VISIBLE
                }
            })
            .autoCancel(false)
            .start()
    }

    private fun unExpand() {
        AnimationUtils.objectAnimationOfX(mLeft, 0f)
            .addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator?, isReverse: Boolean) {
                    mRightBtnGroup.visibility = View.INVISIBLE
                }
            })
            .autoCancel(false)
            .start()
    }

    override fun onClick(parent: GestureRecyclerView, event: MotionEvent) {
        mListener?.let {
            if (ViewUtils.hintView(mDeleteBtn, event)) {
                alertConfirm(message = ResourcesUtils.getString(R.string.delete_confirm)) { delete ->
                    if (delete) {
                        it.onCallbackOtherEvent(DELETE_EVENT, data.copy())
                    }
                    unExpand()
                }
                return@let
            } else {
                it.onCallbackOtherEvent(EDIT_EVENT, data.copy())
            }
            unExpand()
        }
    }

    override fun onLongPress(parent: GestureRecyclerView, event: MotionEvent) {

    }

    override fun onDoubleClick(parent: GestureRecyclerView, event: MotionEvent) {

    }

    override fun onDown(parent: GestureRecyclerView, event: MotionEvent) {

    }
}